{extend name="../application/index/view/index/base.html" /}

{block name="main-content"}

<script src="__PUBLIC__/js/bootstrap.min.js"></script>
<script src="__PUBLIC__/js/template-web.js"></script>
<script src="__PUBLIC__/layer/layer.js"></script>


<link rel="stylesheet" href="__PUBLIC__/zTree_v3-master/css/zTreeStyle/zTreeStyle.css" type="text/css">
<!--<script type="text/javascript" src="__PUBLIC__/zTree_v3-master/js/jquery-1.4.4.min.js"></script>-->
<script type="text/javascript" src="__PUBLIC__/zTree_v3-master/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="__PUBLIC__/zTree_v3-master/js/jquery.ztree.excheck.js"></script>
<script type="text/javascript" src="__PUBLIC__/zTree_v3-master/js/jquery.ztree.exedit.js"></script>
<div class="breadcrumbs" id="breadcrumbs">
	<script type="text/javascript">
        try {
            ace.settings.check('breadcrumbs', 'fixed')
        } catch (e) {
        }
	</script>

	<ul class="breadcrumb">
		<li>
			<i class="icon-home home-icon"></i>
			<a href="#">权限控制</a>
		</li>
		<li class="active">分配权限</li>
	</ul><!-- .breadcrumb -->

	<div class="nav-search" id="nav-search">
		<form class="form-search">
									<span class="input-icon">
										<input type="text" placeholder="Search ..." class="nav-search-input"
											   id="nav-search-input" autocomplete="off"/>
										<i class="icon-search nav-search-icon"></i>
									</span>
		</form>
	</div><!-- #nav-search -->
</div>
<div class="page-content">
	<div class="page-header">
		<h1>
			分配权限
			<small>
				<i class="icon-double-angle-right"></i>
				<span id="title"></span>
			</small>
		</h1>
	</div><!-- /.page-header -->

	<div class="row">
		<ul id="tree" class="ztree"></ul>
		<input class="btn btn-primary" type="button" id="check" value="提交">
	</div><!-- /.row -->
</div><!-- /.page-content -->
<script>
    $(function () {

        var b = $('#tree');
        var ruleDatas = null;

        //树结构配置
        var setting = {
            view: {
                dblClickExpand: false,
                showline: true,
                selectedMulti: false,
                showIcon: false,
            },
            check: {
                enable: true,
                chkStyle: "checkbox",
                chkboxType: {"Y": "ps", "N": "ps"}
            },
            data: {
                key: {
                    title: "title"
                },
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "pId",
                    rootPId: ''
                }
            },
            callback: {
                beforeClick: function (treeId, treeNode) {
                    var zTree = $.fn.zTree.getZTreeObj("tree");
                    if (treeNode.isParent) {
                        zTree.expandNode(treeNode);
                        return false;
                    } else {
                        demoIframe.attr("src", treeNode.file + ".html");
                        return true;
                    }
                }
            }

        };

        //获取所有权限，判断权限是否勾选
        function getData() {
            $.get('/index.php/index/auth/getRules', function (data) {
                ruleDatas = data.data;
                getRulesData();
            });
        }

        //获取用户组权限
        function getRulesData() {
            $.get('/index.php/index/auth/getRulesGroupId', {"group_id": Request.group_id}, function (rules) {
                var ruleArray = rules.data.rules.split(",");
                $("#title").html('为' + rules.data.title + '分配权限');
                var zNodes = [];
                var data = ruleDatas;
                for (var i in data) {
                    if ($.inArray(data[i].id + "", ruleArray) != -1) {
                        var tree = {id: data[i].id, pId: data[i].pid, name: data[i].title, open: true, checked: true};
                    } else {
                        var tree = {id: data[i].id, pId: data[i].pid, name: data[i].title, open: true}
                    }
                    zNodes.push(tree);
                }
                b = $.fn.zTree.init(b, setting, zNodes);
            });
        }

        //获取url传递的参数
        function GetRequest() {
            var url = window.location.search;
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        }
        Request = GetRequest();

        //点击提交，确认分配权限
        $("#check").click(function () {
            var rules = [];
            var treeObj = $.fn.zTree.getZTreeObj("tree");
            var nodes = treeObj.getCheckedNodes(true);
            for (var i in nodes) {
                rules.push(nodes[i].id)

            }
            rule = rules.join(',');
            param = {
                id: Request.group_id,
                rules: rule
            }
            $.post('/index.php/index/auth/updateRules', param, function (data) {
                if (data.code == 200) {
                    layer.msg("设置权限成功", {time: 2000});
                    location.href = 'group.html'
                }else{
                    layer.msg("设置权限失败", {time: 2000});
				}
            });

        });

        //初始化函数
        getData();
    })
</script>
{/block}
